package etnorservice.etnor_service.service.impl;

import etnorservice.etnor_service.domain.po.SystemMonitor;
import etnorservice.etnor_service.service.SystemMonitorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import oshi.SystemInfo;  // 修改这里的导入
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.hardware.NetworkIF;
import oshi.util.Util;

import java.util.List;  // 添加这个导入

@Service
@Slf4j
public class SystemMonitorServiceImpl implements SystemMonitorService {

    private final SystemInfo systemInfo;
    private final HardwareAbstractionLayer hardware;

    public SystemMonitorServiceImpl() {
        this.systemInfo = new SystemInfo();
        this.hardware = systemInfo.getHardware();
    }

    @Override
    public SystemMonitor.CpuInfo getCpuInfo() {
        CentralProcessor processor = hardware.getProcessor();
        long[] prevTicks = processor.getSystemCpuLoadTicks();
        // 等待一秒以获取CPU使用率
        Util.sleep(1000);

        SystemMonitor.CpuInfo cpuInfo = new SystemMonitor.CpuInfo();
        cpuInfo.setUsage(processor.getSystemCpuLoadBetweenTicks(prevTicks) * 100);
        cpuInfo.setCoreCount(processor.getLogicalProcessorCount());
        cpuInfo.setModel(processor.getProcessorIdentifier().getName());
        // 某些系统可能不支持温度监控
        try {
            cpuInfo.setTemperature((int) hardware.getSensors().getCpuTemperature());
        } catch (Exception e) {
            log.warn("无法获取CPU温度信息");
        }

        return cpuInfo;
    }

    @Override
    public SystemMonitor.MemoryInfo getMemoryInfo() {
        GlobalMemory memory = hardware.getMemory();

        SystemMonitor.MemoryInfo memoryInfo = new SystemMonitor.MemoryInfo();
        memoryInfo.setTotal(memory.getTotal());
        memoryInfo.setUsed(memory.getTotal() - memory.getAvailable());
        memoryInfo.setFree(memory.getAvailable());
        memoryInfo.setUsage(((double) memoryInfo.getUsed() / memoryInfo.getTotal()) * 100);

        return memoryInfo;
    }

    @Override
    public SystemMonitor.NetworkInfo getNetworkInfo() {
        List<NetworkIF> networkIFs = hardware.getNetworkIFs();  // 修改这里

        SystemMonitor.NetworkInfo networkInfo = new SystemMonitor.NetworkInfo();
        long totalBytesRecv = 0;
        long totalBytesSent = 0;

        for (NetworkIF net : networkIFs) {
            totalBytesRecv += net.getBytesRecv();
            totalBytesSent += net.getBytesSent();
        }

        networkInfo.setTotalDownload(totalBytesRecv);
        networkInfo.setTotalUpload(totalBytesSent);

        // 计算速度需要两次采样
        Util.sleep(1000);
        long newBytesRecv = 0;
        long newBytesSent = 0;

        for (NetworkIF net : networkIFs) {
            net.updateAttributes();
            newBytesRecv += net.getBytesRecv();
            newBytesSent += net.getBytesSent();
        }

        networkInfo.setDownloadSpeed((newBytesRecv - totalBytesRecv) / 1024.0 / 1024.0); // MB/s
        networkInfo.setUploadSpeed((newBytesSent - totalBytesSent) / 1024.0 / 1024.0);   // MB/s

        return networkInfo;
    }

    @Override
    public SystemMonitor getFullSystemInfo() {
        SystemMonitor monitor = new SystemMonitor();
        monitor.setCpu(getCpuInfo());
        monitor.setMemory(getMemoryInfo());
        monitor.setNetwork(getNetworkInfo());
        return monitor;
    }
}